﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entitys;

namespace RentHouse.Service
{
    public class LogsAllService : ILogsAllService
    {

        DBModel _dbModel;
        public LogsAllService(DBModel _dbModel)
        {

            this._dbModel = _dbModel;
        }



        public List<LogsAllDTO> GetList()
        {
            //_dbModel.T_AdminUsers.Where(e=>e.Id ==int.Parse(t.UserId)).FirstOrDefault().Name
            List<LogsAllDTO> list = _dbModel.T_LogsAll.Select(t => new LogsAllDTO
            {
                Id = t.Id,
                UserId = t.UserId,
                Message = t.Message,
                CreateDateTime = t.CreateDateTime,
                ControllerName = t.ControllerName,
                ActionName = t.ActionName,
                Parameters = t.Parameters,
                Date = t.Date

            }).ToList();
            foreach (LogsAllDTO item in list)
            {
                int i = int.Parse(item.UserId);
                item.UserId = _dbModel.T_AdminUsers.Where(e => e.Id == i).FirstOrDefault().Name;
            }
            return list;

        }


        public string Add(LogsAllDTO t) {

            T_LogsAll t_LogsAll = new T_LogsAll()
            {
                
                Message = t.Message,
                CreateDateTime = t.CreateDateTime,
                ControllerName = t.ControllerName,
                ActionName = t.ActionName,
                Parameters = t.Parameters,
                Date = t.Date
            };
            t_LogsAll.UserId = _dbModel.T_AdminUsers.Where(e => e.Name == t.UserId).FirstOrDefault().Id.ToString();
            _dbModel.T_LogsAll.AddOrUpdate(t_LogsAll);

            int i = _dbModel.SaveChanges();

            if (i <= 0)
            {
                return "error";
            }
            else
            {
                return "succeed";
            }
        }

    }
}
